package com.deng.hadoop.mapreduce.demo.weather;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * Created By DengLibin
 * Date: 19-4-3
 * Time: 上午10:34
 * Desc: map输出key的排序比较器（决定了再送入reduce之前key的排序规则）
 */
public class WeatherSortComparator extends WritableComparator {

    public WeatherSortComparator(){
        super(TianQi.class, true);
    }

    /**
     * 定义排序规则 按年月正序(不需要天) 温度倒序
     * @param a
     * @param b
     * @return
     */
    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        TianQi t1 = (TianQi) a;
        TianQi t2 = (TianQi) b;
        System.out.println("year:"+t1.getYear()+",month:"+t1.getMonth()+",day:"+t1.getDay()+",temperature:"+t1.getTemperature());
        int x = Integer.compare(t1.getYear(), t2.getYear());
        if(x == 0){ //年份相等
            x = Integer.compare(t1.getMonth(), t2.getMonth());
            if(x == 0){//月份也相等
                x = Integer.compare(t2.getTemperature(), t1.getTemperature()); //温度倒序
            }
        }
        return x;
    }
}
